home *** CD-ROM | disk | FTP | other *** search
- #include "PaperJuggling.h"
- #include <StandardFile.h>
- #include <Folders.h>
-
- // This routine just calls WriteJuggleToFile with the fsspec in the juggle
- OSErr SaveJuggle(JuggleHandle aJuggle)
- {
- FSSpec fileSpec;
-
- fileSpec = (*aJuggle)->fileSpec;
- return WriteJuggleToFile(&fileSpec, aJuggle);
- }
-
- // Man, it's a pain when people want to replace an existing file. Sheesh!
- OSErr JuggleToExistingFile(FSSpec *fileSpec, JuggleHandle aJuggle)
- {
- OSErr err;
- unsigned long tempLong;
- short tempRefNum;
- Str255 tempName;
- FSSpec tempSpec;
- FInfo finderStuff;
-
- /* Make a unique file name */
- GetDateTime(&tempLong);
- NumToString(tempLong, tempName);
-
- /* locate the temporary items folder */
- err = FindFolder(fileSpec->vRefNum, kTemporaryFolderType, kCreateFolder,
- &tempRefNum, (long *)&tempLong);
- if(err == noErr)
- {
- /* Make a new FSSpec */
- err = FSMakeFSSpec(tempRefNum, tempLong, tempName, &tempSpec);
- if(err == noErr || err == fnfErr)
- {
- /* Create the temp file */
- err = FSpCreate(&tempSpec, 'trsh', 'trsh', smSystemScript);
- if(err == noErr)
- {
- /* OK, we have a new temp file, let's write to it */
- err = WriteJuggleToFile(&tempSpec, aJuggle);
- if(err == noErr)
- {
- /* Lookin' good. Now, we need to exchange the data, set the Finder
- Info of the old (now new) file, and delete the temp file */
- err = FSpExchangeFiles(&tempSpec, fileSpec);
- if(err == noErr)
- {
- /* First get the existing finder Info */
- err = FSpGetFInfo(fileSpec, &finderStuff);
- if(err == noErr)
- {
- /* then set it to our type and creator */
- finderStuff.fdType = kJuggleFileType;
- finderStuff.fdCreator = kJuggleCreatorType;
- /* Don't care about this error: we did our best, by golly */
- FSpSetFInfo(fileSpec, &finderStuff);
- }
- }
- }
- /* We successfully created the temp file, so now delete it,
- whatever else may have happened. If we can't, there's
- nothing we can do about it, so this error is ignored */
- FSpDelete(&tempSpec);
- }
- }
- }
-
- return err;
- }
-
- /* It's much easier just to create a new file */
- OSErr JuggleToNewFile(FSSpec *fileSpec, JuggleHandle aJuggle)
- {
- OSErr err;
-
- err = FSpCreate(fileSpec, kJuggleCreatorType, kJuggleFileType, smSystemScript);
- if(err == noErr)
- {
- err = WriteJuggleToFile(fileSpec, aJuggle);
- /* If there were errors, delete the file */
- if(err != noErr)
- FSpDelete(fileSpec);
- }
- return err;
- }
-
- // Writes the juggle to the given FSSpec
- OSErr WriteJuggleToFile(FSSpec *fileSpec, JuggleHandle aJuggle)
- {
- JugglePointer jugPtr;
- OSErr err;
- long writeSize;
- short refNum;
-
- HLock((Handle)aJuggle);
- jugPtr = *aJuggle;
-
- /* First open the file */
- err = FSpOpenDF(fileSpec, fsWrPerm, &refNum);
- if(err == noErr)
- {
- // write numjugglers and numCounts out
- writeSize = sizeof(short);
- err = FSWrite(refNum, &writeSize, &jugPtr->numJugglers);
- if(err == noErr)
- {
- writeSize = sizeof(short);
- err = FSWrite(refNum, &writeSize, &jugPtr->numCounts);
- if(err == noErr)
- {
- // Write the block of throws
- writeSize = GetHandleSize((Handle)jugPtr->theThrows);
- HLock((Handle)jugPtr->theThrows);
- err = FSWrite(refNum, &writeSize, *jugPtr->theThrows);
- HUnlock((Handle)jugPtr->theThrows);
- }
- }
- /* Close the file. If there were errors, the file is deleted by the caller, who
- created it in the first place */
- FSClose(refNum);
- }
-
- HUnlock((Handle)aJuggle);
- return err;
- }
-
- /* Opens the file indicated by fileSpec, reads the juggle in,
- and creates a new document, showing the window. */
- OSErr OpenJuggleFile(FSSpec *fileSpec, WindowPtr *wind)
- {
- OSErr err;
- long readSize;
- short refNum, numJugglers, numCounts;
-
- /* Open the file and read in just the size (numJugglers and numCounts)
- so we can get the size of the Juggle we'll need. */
- err = FSpOpenDF(fileSpec, fsWrPerm, &refNum);
- if(err == noErr)
- {
- /* Read in num jugglers */
- readSize = sizeof(short);
- err = FSRead(refNum, &readSize, &numJugglers);
- if(err == noErr)
- {
- /* Read in num counts */
- readSize = sizeof(short);
- err = FSRead(refNum, &readSize, &numCounts);
- if(err == noErr)
- {
- // range check
- if(numJugglers > 0 && numJugglers <= kMaxJugglers &&
- numCounts > 0 && numCounts % 2 == 0)
- {
- WindowPtr newWind;
- // !!! Check the file size
-
- // Set up the newJuggle
- newWind = AppNew(numJugglers, numCounts);
- if(newWind != nil)
- {
- JuggleHandle aJuggle;
- HandHandle throws;
-
- // Get the juggle, assume sucess
- aJuggle = GetWindowJuggle(newWind);
- throws = (*aJuggle)->theThrows;
-
- // read in throws data
- readSize = numCounts * numJugglers * sizeof(Hand);
- HLock((Handle)throws);
- err = FSRead(refNum, &readSize, *throws);
- HUnlock((Handle)throws);
-
- // data is restored. Now we need to do the bookkeeping
- // save the fileSpec and maybe the window
- (*aJuggle)->fileSpec = *fileSpec;
- if(wind != nil)
- *wind = newWind;
-
- /* Rename the window to match the file name */
- SetWTitle(newWind, fileSpec->name);
-
- /* clear the dirty flag */
- (*aJuggle)->dirty = false; /* no need to save unless it changes */
-
- // Finally, rebuild throws and force a redraw
- RebuildThrowsPict(aJuggle);
- SetPort(newWind);
- EraseRect(&newWind->portRect);
- InvalRect(&newWind->portRect);
- }
- }
- }
- }
- // Close the file
- FSClose(refNum);
- }
- return err;
- }
-
-
-
-
-
-
-